package panda.query.tree.index;

import panda.index.Index;
import panda.metainfo.IndexToken;
import panda.query.scan.Scanner;
import panda.query.scan.TableScanner;
import panda.query.scan.index.IndexSelectScanner;
import panda.query.struct.Attribute;
import panda.query.struct.Constant;
import panda.query.tree.QueryTreeNode;
import panda.record.Schema;
import panda.transaction.Transaction;

/**
 * A query tree node that performs <i>SELECT</i> utilizing index
 * @author Tian Yuan
 *
 */
public class IndexSelectNode implements QueryTreeNode{
	private QueryTreeNode src;
	private IndexToken it;
	private Constant val;
	private int op;
	
	public IndexSelectNode(QueryTreeNode src, IndexToken it, Constant val, int op, Transaction tx) {
		this.src = src;
		this.it = it;
		this.val = val;
		this.op = op;
	}
	
	public Scanner open() {
		TableScanner ts = (TableScanner) src.open();
		Index idx = it.open();
		return new IndexSelectScanner(idx, val, ts, op);
	}

	public int blockAccessed() {
		return it.blockAccessed() + outputSize();
	}

	public int distinctSize(Attribute attr) {
		return it.distinctSize(attr);
	}

	public Schema getSchema() {
		return src.getSchema();
	}

	public int outputSize() {
		return it.outputSize();
	}

}
